<?php
/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/5/18
 * Time: 16:06
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Base_Model extends CI_Model {

    protected $mTable;
    protected $mPkId = 'id';

    public function __construct()
    {
        $this->db = _get_db('default');
    }

    public function prefix()
    {
        return $this->db->dbprefix;
    }

    public function table($table = '')
    {
        if (!$table) {
            $table = $this->mTable;
        }
        return $this->db->protect_identifiers($table, TRUE);
    }

    public function set_table($table)
    {
        $this->mTable = $table;
        return $this;
    }

//    public function execute($sql)
//    {
//        return $this->db->query($sql);
//    }

    /**
     * 插入数据
     * @param array $data 要插入的数据
     * @return mixed
     */
    public function add($data){
        return $this->db->insert($this->mTable,$data);

        return $this->db->last_query();
    }

    /**
     * 编辑数据
     * @param array $data where条件
     * @return mixed
     */
    public function edit($data,$arg){
         return $this->db->update($this->mTable,$data,$arg);

         return $this->db->last_query();
    }

    /**
     * 删除数据
     * @param array $data where条件
     * @return mixed
     */
    public function remove($data){
        return $this->db->delete($this->mTable,$data);
    }

    /**
     * 根据字段条件查询 一条/多条数据
     * @param string $fields  查询字段
     * @param array $arg  where参数
     * @param int $isMore 0查询一条/1查询多条
     * @return mixed
     */
    public function getByFieldQueryData($fields = '*',$arg,$isMore = 0)
    {
        if($isMore){
            $result = $this->db->select($fields)
                ->from($this->mTable)
                ->where($arg)
                ->get()
                ->result_array();
            return $result;
        }else{
            $result = $this->db->select($fields)
                ->from($this->mTable)
                ->where($arg)
                ->get()
                ->row_array();
            return $result;
        }

//        return $this->db->last_query(); 打印sql语句
    }

    /**
     * 自定义字段值 一条/多条数据
     * @param string $fields  查询字段
     * @param int $isMore 0查询一条/1查询多条
     * @return mixed
     */
    public function getData($fields= '*',$isMore = 0){
        if($isMore){
            $result = $this->db->select($fields)
                ->from($this->mTable)
                ->get()
                ->result_array();
            return $result;
        }else{
            $result = $this->db->select($fields)
                ->from($this->mTable)
                ->get()
                ->row_array();
            return $result;
        }
    }

    /**
     * 模糊条件查询
     * $this->db->where('字段名','字段值');
     * $this->db->delete('表名');
     * $this->db->update('表名');
     * $where 判断是否启用where条件查询
     */
    public function search($fields = '*',$where,$searchArg,$per_nums,$start_position)
    {
        if($where){
            $result = $this->db->select($fields)
                ->from($this->mTable)
                ->where($where)
                ->like(
                    $searchArg['field'],
                    $searchArg['arg'],
                    $searchArg['side']
                )
                ->limit($per_nums,$start_position)
                ->get()
                ->result_array();

            $result2['total_num'] = $this->db->select($fields)
                ->from($this->mTable)
                ->like(
                    $searchArg['field'],
                    $searchArg['arg'],
                    $searchArg['side']
                )
                ->get()
                ->result_array();

            $result2[] = $result;
        }else{
            $result = $this->db->select($fields)
                ->from($this->mTable)
                ->like(
                    $searchArg['field'],
                    $searchArg['arg'],
                    $searchArg['side']
                )
                ->limit($per_nums,$start_position)
                ->get()
                ->result_array();

            $result2['total_num'] = $this->db->select($fields)
                ->from($this->mTable)
                ->like(
                    $searchArg['field'],
                    $searchArg['arg'],
                    $searchArg['side']
                )
                ->get()
                ->result_array();

            $result2[] = $result;
        }



        return $result2;
//        return $this->db->last_query();

    }

    /**
     * 分页函数
     * @param string $tablename 表名
     * @param string $per_nums  每页的数据量
     * @param string $start_position    起始位置
     * @return array
     */
    public function page($tablename,$where,$per_nums,$start_position){
        if($where){
            $this->db->where($where)->limit($per_nums,$start_position);
            $query=$this->db->get($tablename);
            $data = $query->result_array();
//            return $this->db->last_query();

//        return $this->db->last_query();
            $data2['total_num']=$this->db->count_all($tablename);
            $data2[]=$data;
        }else{
            $this->db->limit($per_nums,$start_position);
            $query=$this->db->get($tablename);
            $data = $query->result_array();
//        return $this->db->last_query();
            $data2['total_num']=$this->db->count_all($tablename);
            $data2[]=$data;
        }

        return $data2;

    }




}